package com.example.ymm.modules1.service.impl.user;

import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.my.user.DbIntegralLog;
import com.example.ymm.modules1.entity.my.user.DbRewards;
import com.example.ymm.modules1.entity.my.user.DbUser;
import com.example.ymm.modules1.entity.my.user.DbUserRewards;
import com.example.ymm.modules1.mapper.my.user.DbUserRewardsMapper;
import com.example.ymm.modules1.query.DbUserRewardsQueryCriteria;
import com.example.ymm.modules1.service.dd.DingMiniNoticeService;
import com.example.ymm.modules1.service.user.DbIntegralLogService;
import com.example.ymm.modules1.service.user.DbRewardsService;
import com.example.ymm.modules1.service.user.DbUserRewardsService;
import com.example.ymm.modules1.service.user.DbUserService;
import com.example.ymm.util.QueryHelpPlus;
import com.example.ymm.util.UtilValidate;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class DbUserRewardsServiceImpl extends BaseServiceImpl<DbUserRewardsMapper, DbUserRewards> implements DbUserRewardsService {

    @Autowired
    DbIntegralLogService dbIntegralLogService;
    @Autowired
    DbRewardsService dbRewardsService;
    @Autowired
    DbUserService dbUserService;
    @Autowired
    DingMiniNoticeService dingMiniNoticeService;

    @Override
    public TableList<DbUserRewards> list(DbUserRewardsQueryCriteria criteria, YmPageDto pageDto) {
        getPage(pageDto);
        List<DbUserRewards> maps = baseMapper.getUserRewardsList(criteria);
        PageInfo<DbUserRewards> pageInfo = new PageInfo<>(maps);
        TableList<DbUserRewards> tableList=new TableList<>(pageInfo);
        return tableList;
    }

    public List<DbUserRewards> queryAll(DbUserRewardsQueryCriteria criteria) {
        List<DbUserRewards> result =baseMapper.selectList(QueryHelpPlus.getPredicate(DbUserRewards.class, criteria));
        return result;
    }


    @Override
    public DbUserRewards addToUser(DbUserRewards userRewards) {
        DbRewards rewards = dbRewardsService.getById(userRewards.getRewardsId());

        DbUser user = dbUserService.getById(userRewards.getUserId());

        userRewards.setUserId(user.getUserId());
        userRewards.setUserName(user.getUserName());
        saveOrUpdate(userRewards);

        DbIntegralLog integralLog=new DbIntegralLog();
        integralLog.setIntegral(rewards.getIntegral());
        integralLog.setUserId(user.getUserId());
        integralLog.setUserName(user.getUserName());
        integralLog.setType(rewards.getType());
        integralLog.setNote(rewards.getName());
        dbIntegralLogService.saveOrUpdate(integralLog);
        //类型 1奖励 2处罚 3系统回收
        //给积分
        if(rewards.getType().equals(1)){
            user.setIntegral(user.getIntegral()+ rewards.getIntegral());
            dingMiniNoticeService.sendIntegralMsg(integralLog,userRewards,rewards);
        }
        if(rewards.getType().equals(2)||rewards.getType().equals(3)){
            if(user.getIntegral()> rewards.getIntegral()){
                user.setIntegral(user.getIntegral()- rewards.getIntegral());
            }else{
                user.setIntegral(0);
            }

            dingMiniNoticeService.sendIntegralMsg(integralLog,userRewards,rewards);
        }
        dbUserService.saveOrUpdate(user);

        return userRewards;
    }


    @Override
    public void importUserRewardsExcel(List<DbUserRewards> list) {
        for(DbUserRewards userRewards: list){
            DbUser user = dbUserService.findByUserName(userRewards.getUserName());
            DbRewards rewards = dbRewardsService.findByName(userRewards.getName());
            if(UtilValidate.isEmpty(rewards)||UtilValidate.isEmpty(user)){
                continue;
            }
            userRewards.setRewardsId(rewards.getRewardsId());
            userRewards.setUserId(user.getUserId());
            addToUser(userRewards);
        }
    }
}
